package com.bms.util;

public class PageUtil {

	private int pageSize;// 每页数据的条数
	private int currenPage;// 当前页
	private int totalSize;// 总条数

	//知道当前页
	public PageUtil(int pageSize, int currenPage, int totalSize) {
		this.pageSize = pageSize;
		this.currenPage = currenPage;
		this.totalSize = totalSize;
	}

	//首页
	public PageUtil(int pageSize, int totalSize) {
		this(pageSize, 1, totalSize);
		this.pageSize = pageSize;
		this.totalSize = totalSize;
	}

	//总页数
	public int getTotalPage(){
		int total=this.totalSize/this.pageSize;
		if(this.totalSize%this.pageSize!=0){
			total=total+1;
		}
		return total;
	}
	
	//上一页
	public int prePage(){
		int prePage=this.currenPage-1;
		//如果是首页
		if(prePage<1){
			prePage=1;
		}
		return prePage;
	}
	
	//下一页
	public int nextPage(){
		int nextPage=this.currenPage+1;
		//如果是末页
		if(nextPage>getTotalPage()){
			nextPage=getTotalPage();
		}
		return nextPage;
	}
	
	//末页
	public int lastPage(){
		return getTotalPage();
	}
	
	/**
	 * 数据显示的起始索引(从0开始)
	 * @return
	 */
	public int beginIndex(){
		int index=(this.currenPage-1)*pageSize;
		return index;
	}
	
	/**
	 * 数据显示的终止索引
	 * @return
	 */
	public int endIndex(){
		int end=this.currenPage*pageSize;
		//如果是末页
		if(end>this.totalSize){
			end=this.totalSize;
		}
		return end;
	}
	
}
